/*******************************************
  Jose Andres Lara Vecino

  Author       : $Author: cainlara@gmail.com $
  Last CheckIn : $Date: 2012-08-22 18:35:06 +0000 (Wed, 22 Aug 2012) $
  Revision     : $Revision: 100 $
*******************************************/

package generico;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.JPopupMenu;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import utilidad.IPropiedades;

/**
 * Super clase para el componente tabla.
 * 
 * @author jose.lara 06.06.2012
 */
public abstract class GenericoTabla extends JTable {
    private static final long serialVersionUID = 196383275516285706L;
    
    private JPopupMenu menuContextual       = null;
    
    public GenericoTabla() {
        super();
        agregarMenuContextual();
        getTableHeader().setReorderingAllowed(false);
        agregarSelectionListener();
        setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    }
    
    protected abstract List<GenericoAccion> getAcciones();
    
    @SuppressWarnings("rawtypes")
    protected abstract GenericoTablaModelo getModelo();
    
    protected abstract void actualizarAcciones();
    
    private JPopupMenu getMenuContextual() {
        if(menuContextual == null) {
            menuContextual = new JPopupMenu();
        }
        actualizarMenuContextual();
        
        return menuContextual;
    }
    
    private void actualizarMenuContextual() {
        if(menuContextual != null) {
            menuContextual = new JPopupMenu();
            
            for(GenericoAccion accion : getAcciones()) {
                menuContextual.add(accion);
            }
        }
    }
    
    private void mostrarMenuContextual(MouseEvent e) {
        if(e.isPopupTrigger() && getAcciones() != null && !getAcciones().isEmpty()) {
            getMenuContextual().show(e.getComponent(), e.getX(), e.getY());
        }
    }
    
    private void agregarMenuContextual() {
        addMouseListener(new MouseAdapter() {
            
            @Override
            public void mouseReleased(MouseEvent e) {
                mostrarMenuContextual(e);
            }
            
            @Override
            public void mousePressed(MouseEvent e) {
                mostrarMenuContextual(e);
            }
        });
    }
    
    private void agregarSelectionListener() {
        getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                if(!e.getValueIsAdjusting()) {
                    actualizarAcciones();
                }
            }
        });
    }
    
    protected void actualizarDatos() {
        getModelo().actualizarDatos();
        firePropertyChange(IPropiedades.ACTUALIZAR, false, true);
    }
}